//
//  WKPhotoPickerVC.swift
//  NewDelivery
//
//  Created by 天下宅 on 2016/11/9.
//  Copyright © 2016年 天下宅. All rights reserved.
//

import UIKit



class WKPhotoPickerVC: BaseVC {
    var collctionView:UICollectionView!
    var hintLabel:UILabel!
    var cvp:PhotoCVPresenter!
    var maxCount:Int = 4
    var block:photosBlock?
    var images = [UIImage]()
    
    
    var showView:UIView = {
        let v = UIView.init(frame: CGRect.init(x: 0, y: 0, width: screenWidth, height: screenHeight))
        v.backgroundColor = UIColor.black.withAlphaComponent(0.5)
        return v
        
    }()
    var showIV:UIImageView = {
        let iv = UIImageView()
        iv.bounds = CGRect.init(x: 0, y: 0, width: screenWidth * 0.8, height: screenHeight * 0.8)
        return iv
    }()
    
    override func viewDidLoad() {
        
        self.view.backgroundColor = UIColor.groupTableViewBackground
        
        let nav = WKNavView.creatNavView(WithTitle: "选择图片", vProtocol: self)
        self.view.addSubview(nav)
        nav.leftTitle = "取消"
        nav.rightTitle = "确定"
        nav.rightBtnEnable = true
        
        
        
        hintLabel = UILabel()
        hintLabel.backgroundColor = UIColor.white
        hintLabel.textColor = UIColor.black
        hintLabel.font = UIFont.systemFont(ofSize: 16)
        self.view.addSubview(hintLabel)
        hintLabel.frame = CGRect.init(x: 0, y: screenHeight - 40, width: screenWidth, height: 40)
        hintLabel.textAlignment = NSTextAlignment.center
        
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.scrollDirection = .vertical
        flowLayout.itemSize = CGSize.init(width: 85, height: 85)
        
        collctionView = UICollectionView.init(frame: CGRect.init(x: 0, y: nav.bottom, width: screenWidth, height: screenHeight - 64 - 40), collectionViewLayout: flowLayout)
        self.view.addSubview(collctionView)
        
        cvp = PhotoCVPresenter.init(WithCV: collctionView, cvc: self)
        cvp.selectImages = images
        cvp.maxCount = 4
        cvp.hintLabel = hintLabel
        
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(tapClick))
        self.showView.addGestureRecognizer(tap)
        
        showIV.center = showView.center
        showView.addSubview(showIV)
    }
    
    
    func displayShowView(_ image:UIImage)
    {
        
        showView.alpha = 0
        showIV.image = image
        self.view.addSubview(showView)
        UIView.animate(withDuration: 0.3, animations: {
            self.showView.alpha = 1
        })
    }
    
    func tapClick() {
        UIView.animate(withDuration: 0.3, animations: {
            self.showView.alpha = 0
        }, completion: {(flag) in
            self.showView.removeFromSuperview()
        })
        
    }
    
    override func navBtnClick(WithType type: WKNavViewClickType) {
        switch type {
        case .left:
            break
        case .right:
            if block != nil
            {
                block!(self.cvp.selectImages)
            }

        }
        self.dismiss(animated: true, completion: nil)

    }
}
